package com.zzsy.shoppingpro.shoppingcart.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zzsy.shoppingpro.app.application;
import com.zzsy.shoppingpro.home.bean.GoodsBean;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/8/23 0023.
 * 数据存储类
 */

public class CartProvider {

    public static final String JSON_CART = "json_cart";
    private Context context;
    //优化过的HashMap集合
    private SparseArray<GoodsBean> datas;

    private static CartProvider cartProvider;

    public CartProvider(Context context) {
        this.context = context;
        datas = new SparseArray<>(100);
        listToSparse();
    }

    public static CartProvider getInstance(){
        if(cartProvider == null){
            cartProvider = new CartProvider(application.getApp());
        }

        return cartProvider;
    }

    /**
     * 将集合中的数据 保存到SparseArray中
     * */
    private void listToSparse() {
        List<GoodsBean> carts = getAllData();
        //放到sparseArry中
        if(carts != null && carts.size() > 0){
            for (int i = 0; i < carts.size(); i++){
                GoodsBean goodsBean = carts.get(i);
                datas.put(Integer.parseInt(goodsBean.getProduct_id()),goodsBean);
            }
        }
    }

    /**
     * 将数据转换为list
     * */
    private List<GoodsBean> parsesToList(){
        List<GoodsBean> carts = new ArrayList<>();
        if(datas != null && datas.size() > 0){
            for(int i =0;i< datas.size();i++){
                GoodsBean goodsBean = datas.valueAt(i);
                carts.add(goodsBean);
            }
        }
        return carts;
    }

    private List<GoodsBean> getAllData() {
        return getDataFromLocal();
    }

    /**
     * 本地获取json数据，并且通过Gson解析成list列表数据
     * */
    public List<GoodsBean> getDataFromLocal() {
        List<GoodsBean> carts = new ArrayList<>();
        //从本地获取缓存数据
        String savaJson = CacheUtils.getString(context,JSON_CART);
        if (!TextUtils.isEmpty(savaJson)){
            //把数据转换成列表
            carts = new Gson().fromJson(savaJson,new TypeToken<List<GoodsBean>>(){}.getType());
        }
        return carts;
    }

    /**
     * 添加数据
     * */
    public void addData(GoodsBean cart){
        //添加数据
        GoodsBean tempCart = datas.get(Integer.parseInt(cart.getProduct_id()));
        if(tempCart != null){
            tempCart.setNumber(tempCart.getNumber() + cart.getNumber());
        }else{
            tempCart = cart;
            tempCart.setNumber(1);
        }

        datas.put(Integer.parseInt(tempCart.getProduct_id()),tempCart);

        //保存数据
        commit();
    }

    /**
     * 保存数据
     *
     * */
    private void commit() {
        //把parseArray转换成list
        List<GoodsBean> carts = parsesToList();
        //转换成string
        String json = new Gson().toJson(carts);
        //保存
        CacheUtils.putString(context,JSON_CART,json);

    }

    /**
     * 删除数据
     * */
    public void deleteData(GoodsBean goodsBean){
        //删除数据
        datas.delete(Integer.parseInt(goodsBean.getProduct_id()));

        //保存数据
        commit();

    }

    /**
     * 更新数据
     * */
    public void updateData(GoodsBean goodsBean){
        //修改数据
        datas.put(Integer.parseInt(goodsBean.getProduct_id()),goodsBean);
        //保存数据
        commit();

    }



    /**
     * 查找数据
     * */
    public GoodsBean findData(GoodsBean goodsBean){

        GoodsBean goodsBeans = datas.get(Integer.parseInt(goodsBean.getProduct_id()));
        if(goodsBeans != null){
            return goodsBean;
        }
        return null;
    }

}
